public class Thread02 {
    public static void main(String[] args) {

        MyThread thread = new MyThread();
        Thread t1 = new Thread(thread);
        Thread t2 = new Thread(thread);

        t1.setName("线程1");
        t2.setName("线程2");

        t1.start();
        t2.start();
    }
}
class MyThread implements Runnable{

    Object obj = new Object();
    private int n = 1;
    @Override
    public void run() {
        while (n <= 100) {
            synchronized (obj) {
                obj.notify();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "打印" + n);
                  n++;
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
